import wmi

class Win_shell (object):
    def __init__ (self,ip):
        self.ip = ip
        
    def open_ssh(self):
        c = wmi.WMI (computer = self.ip,user = r'.\admin',password = '2018.pw.')
        
        copy = r'net use \\172.25.1.110\public\temp liangzx3 /user:root & mkdir c:\"Program Files"\OpenSSH-Win64 & copy \\172.25.1.110\public\temp\OpenSSH-Win64 c:\"Program Files"\OpenSSH-Win64'
        
        path = r'cd c:\Program Files\OpenSSH-Win64'
        
        shell = r'powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1'
        
        port = r'netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22'
        
        sshd = r'net start sshd & sc config sshd start= auto'

        cmd = r'cmd /c {} & {} & {} & {} & {}'.format (copy,path,shell,port,sshd)

        c.Win32_Process.Create (CommandLine = cmd)
    
# from assets.models import Asset
# a = Asset.objects.get (IP = '172.25.1.105')
# a.user = s
# a.save ()

# 批量更新
# Asset.objects.filter(IP__contains=ip).update(user = s)